package xyz.zhouzhousag.server.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import xyz.zhouzhousag.server.dao.AdminUserRoleRelDao;
import xyz.zhouzhousag.server.pojo.entity.AdminUserRoleRel;
import xyz.zhouzhousag.server.service.AdminUserRoleRelService;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 角色用户关系表(AdminUserRoleRel)表服务实现类
 *
 * @author Yin.Liu8
 * @since 2021-09-26 16:21:07
 */
@Service("adminUserRoleRelService")
public class AdminUserRoleRelServiceImpl implements AdminUserRoleRelService {
    @Resource
    private AdminUserRoleRelDao adminUserRoleRelDao;

    /**
     * 通过ID查询单条数据
     *
     * @param adminUserRoleRelId 主键
     * @return 实例对象
     */
    @Override
    public AdminUserRoleRel queryById(Integer adminUserRoleRelId) {
        return this.adminUserRoleRelDao.queryById(adminUserRoleRelId);
    }

    /**
     * 分页查询
     *
     * @param adminUserRoleRel 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<AdminUserRoleRel> queryByPage(AdminUserRoleRel adminUserRoleRel, PageRequest pageRequest) {
        long total = this.adminUserRoleRelDao.count(adminUserRoleRel);
        return new PageImpl<>(this.adminUserRoleRelDao.queryAllByLimit(adminUserRoleRel, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param adminUserRoleRel 实例对象
     * @return 实例对象
     */
    @Override
    public AdminUserRoleRel insert(AdminUserRoleRel adminUserRoleRel) {
        this.adminUserRoleRelDao.insert(adminUserRoleRel);
        return adminUserRoleRel;
    }

    /**
     * 修改数据
     *
     * @param adminUserRoleRel 实例对象
     * @return 实例对象
     */
    @Override
    public AdminUserRoleRel update(AdminUserRoleRel adminUserRoleRel) {
        this.adminUserRoleRelDao.update(adminUserRoleRel);
        return this.queryById(adminUserRoleRel.getAdminUserRoleRelId());
    }

    /**
     * 通过主键删除数据
     *
     * @param adminUserRoleRelId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer adminUserRoleRelId) {
        return this.adminUserRoleRelDao.deleteById(adminUserRoleRelId) > 0;
    }

    /**
     * 根据用户id获取用户的角色信息
     *
     * @param adminUserId
     * @return
     */
    @Override
    public Set<String> getByAdminUserId(Long adminUserId) {
        return this.adminUserRoleRelDao.getByAdminUserId(adminUserId);
    }
}
